package com.osfw.dbadminer.controller.config;

import com.github.pagehelper.PageInfo;
import com.osfw.dbadminer.entity.config.DbaConfigProcess;
import com.osfw.dbadminer.entity.config.DbaConfigScope;
import com.osfw.dbadminer.service.config.IDbaConfigProcessService;
import com.osfw.dbadminer.service.config.IDbaConfigScopeService;
import com.osfw.framework.annotation.RestfulMessage;
import com.osfw.framework.core.controller.BaseController;
import com.osfw.framework.core.domain.RestfulResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 数据库操作审批流程配置 信息操作处理
 */
@Controller
@RequestMapping("/osfw/dbadminer/config")
public class DbaConfigController extends BaseController {
    private String prefix = "osfw/dbadminer/config";

    @Autowired
    private IDbaConfigProcessService processService;
    @Autowired
    private IDbaConfigScopeService scopeService;


    @RequiresPermissions("dbadminer:config:view")
    @GetMapping()
    public String config() {
        return prefix + "/config";
    }

    @RequiresPermissions("dbadminer:config:view")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 查询数据库操作权限范围配置列表
     */
    @RestfulMessage("查询成功")
    @RequiresPermissions("dbadminer:config:list")
    @PostMapping("/scopeList")
    @ResponseBody
    public PageInfo scopeList(DbaConfigScope scope) {
        startPage(DbaConfigScope.class);
        List<DbaConfigScope> list = scopeService.selectScopeList(scope);
        return getPageInfo(list);
    }

    /**
     * 查询数据库操作审批流程配置列表
     */
    @RestfulMessage("查询成功")
    @RequiresPermissions("dbadminer:config:list")
    @PostMapping("/processList")
    @ResponseBody
    public PageInfo processList(DbaConfigProcess process) {
        startPage(DbaConfigProcess.class);
        List<DbaConfigProcess> list = processService.selectProcessList(process);
        return getPageInfo(list);
    }

    /**
     * 数据库操作权限分配用户
     */
    @RestfulMessage("数据库操作权限分配用户")
    @RequiresPermissions("dbadminer:config:auth")
    @PostMapping("/authUsers")
    @ResponseBody
    public RestfulResult authUsers(Long connectId, String databaseName, String datatableName, String userIds) {
        return toAjax(scopeService.authUsers(connectId, databaseName, datatableName, userIds));
    }

    /**
     * 数据库设置操作审批流程
     */
    @RestfulMessage("数据库设置操作审批流程")
    @RequiresPermissions("dbadminer:config:setup")
    @PostMapping("/setupProcess")
    @ResponseBody
    public RestfulResult setupProcess(Long connectId, String databaseName, String datatableName, String operType, String procKey) {
        return toAjax(processService.setupProcess(connectId, databaseName, datatableName, operType, procKey));
    }

}
